package com.globallogic.vacationplanner.web.controller;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import org.apache.log4j.Logger;

import com.globallogic.vacationplanner.transfer.dto.impl.ProjectDto;
import com.globallogic.vacationplanner.transfer.dto.impl.UserDto;
import com.globallogic.vacationplanner.transfer.service.IProjectService;
import com.globallogic.vacationplanner.web.exceptions.ViewException;
import com.globallogic.vacationplanner.web.helper.ProjectBeanHelper;
import com.globallogic.vacationplanner.web.helper.UserBeanHelper;

@ManagedBean
@SessionScoped
public class ProjectBean extends BaseBean implements Serializable {
	private static final long serialVersionUID = 1L;

	private static Logger LOGGER = Logger.getLogger(ProjectBean.class);
	private static final String CREATE_SUCCESS = "Project creating is success";
	private static final String CREATE_FAILED = "project creating  is failed";
	private ProjectDto projectDto = new ProjectDto();
	private UserDto userDto = new UserDto();
	private ProjectDto edtiProjectDto = new ProjectDto();
	private IProjectService projectService;
	private String resultCreation = null;
	private String showCreationForm = null;
	private List<UserDto> users;
	private List<UserDto> selectedUsers;
	private List<ProjectDto> userMemberedProject = null;
	private List<ProjectDto> userOwnProject = null;
	private List<ProjectDto> allProject;
	private int projectIndex;
	private int projectEditIndex;

	/**
	 * Create the project
	 */
	public String createProject() {
		LOGGER.info("Create project");
		projectService = getServiceContext().getProjectService();
		try {
			projectDto = ProjectBeanHelper.buildProjectDto(this);
			projectService.createProject(projectDto);
			resultCreation = CREATE_SUCCESS;
			showCreationForm = "";
			return "Project created successfully";
		} catch (ViewException ex) {
			resultCreation = CREATE_FAILED;
			showCreationForm = "";
			return "Project created unsuccessfully";
		}
	}

	/**
	 * Getting users list without authorize user
	 * 
	 * @return
	 */
	public List<UserDto> getUsers() {
		if (users == null) {
			projectService = getServiceContext().getProjectService();
			try {
				userDto = new UserBeanHelper().buildUserDto(getUserBean());
			} catch (ViewException e) {
				LOGGER.info("Exception getUsers");
				LOGGER.info("Exception = " + e.getCause());
			}
			users = projectService.fillUsersList(userDto);
		}
		return users;
	}

	public String createNewProject() {
		setShowCreationForm(null);
		resultCreation = null;
		setName(null);
		setDescription(null);
		return "yes";
	}

	/**
	 * Getting all projects list
	 * 
	 * @return
	 */
	public List<ProjectDto> fillTable() {
		if (allProject == null) {
			LOGGER.info("in fillTable()");
			projectService = getServiceContext().getProjectService();
			allProject = projectService.getAllProject();
			LOGGER.info("Get all project");
		}
		return allProject;
	}

	/**
	 * Getting list of User membered projects
	 * 
	 * @return list
	 */
	public List<ProjectDto> fillTableUserMemberdProjects() {
		projectService = getServiceContext().getProjectService();
		try {
			userDto = new UserBeanHelper().buildUserDto(getUserBean());
			userMemberedProject = projectService
					.getUserMemberedProjects(userDto);
		} catch (ViewException e) {
			LOGGER.info("Exception fillTableUserMemberdProjects project");
			LOGGER.info("Exception = " + e.getCause());
		}
		return userMemberedProject;
	}

	/**
	 * Getting list of User own projects
	 * 
	 * @return list
	 */
	public List<ProjectDto> fillTableUserOwnProjects() {
		projectService = getServiceContext().getProjectService();
		try {
			userDto = new UserBeanHelper().buildUserDto(getUserBean());
			userOwnProject = projectService.getUserOwnProjects(userDto);
		} catch (ViewException e) {
			LOGGER.info("Exception fillTableUserOwnProjects project");
			LOGGER.info("Exception = " + e.getCause());
		}
		return userOwnProject;
	}

	public String leaveTheProject() {
		LOGGER.info("in leave project");
		projectService = getServiceContext().getProjectService();
		try {
			userDto = new UserBeanHelper().buildUserDto(getUserBean());
			ProjectDto project = getUserMemberedProject().get(projectIndex);
			projectService.userLeaveProject(project, userDto);
		} catch (ViewException e) {
			LOGGER.info("Exception in leave project");
			LOGGER.info("Exception = " + e.getCause());
		}
		return "yes";
	}

	public String showEditProjectPage() {
		LOGGER.info("in leave project");
		ProjectDto project = getUserOwnProject().get(projectEditIndex);
		setEdtiProjectDto(project);
		return "yes";
	}

	public String editUserProject() {
		LOGGER.info("in leave project");
		projectService = getServiceContext().getProjectService();
		ProjectDto editingProject = getEdtiProjectDto();
		projectService.edtiUserOwnProject(editingProject);
		return "yes";
	}

	public ProjectDto getEdtiProjectDto() {
		return edtiProjectDto;
	}

	public void setEdtiProjectDto(ProjectDto edtiProjectDto) {
		this.edtiProjectDto = edtiProjectDto;
	}

	public int getProjectEditIndex() {
		return projectEditIndex;
	}

	public void setProjectEditIndex(int projectEditIndex) {
		this.projectEditIndex = projectEditIndex;
	}

	public int getProjectIndex() {
		return projectIndex;
	}

	public void setProjectIndex(int projectIndex) {
		this.projectIndex = projectIndex;
	}

	public void setUsers(List<UserDto> users) {
		this.users = users;
	}

	public List<UserDto> getSelectedUsers() {
		return selectedUsers;
	}

	public void setSelectedUsers(List<UserDto> selectedUsers) {
		this.selectedUsers = selectedUsers;
	}

	public List<ProjectDto> getUserMemberedProject() {
		return userMemberedProject;
	}

	public void setUserMemberedProject(List<ProjectDto> userMemberedProject) {
		this.userMemberedProject = userMemberedProject;
	}

	public List<ProjectDto> getUserOwnProject() {
		return userOwnProject;
	}

	public void setUserOwnProject(List<ProjectDto> userOwnProject) {
		this.userOwnProject = userOwnProject;
	}

	public IProjectService getProjectService() {
		return getServiceContext().getProjectService();
	}

	public String getLoginCreator() {
		return projectDto.getLoginCreator();
	}

	public void setLoginCreator(String loginCreator) {
		projectDto.setLoginCreator(loginCreator);
	}

	public String getName() {
		return projectDto.getName();
	}

	public void setName(String name) {
		projectDto.setName(name);
	}

	public String getShowCreationForm() {
		return showCreationForm;
	}

	public void setShowCreationForm(String showCreationForm) {
		this.showCreationForm = showCreationForm;
	}

	public String getDescription() {
		return projectDto.getDescription();
	}

	public void setDescription(String description) {
		projectDto.setDescription(description);
	}

	public String getState() {
		return projectDto.getState();
	}

	public void setState(String state) {
		projectDto.setState(state);
	}

	public List<UserDto> getUserMembersList() {
		return projectDto.getUserMembersList();
	}

	public void setUserMembersList(List<UserDto> userMembersList) {
		projectDto.setUserMembersList(userMembersList);
	}

	public String getResultCreation() {
		return resultCreation;
	}

	public void setResultCreation(String resultCreation) {
		this.resultCreation = resultCreation;
	}

}
